//
//  YHStatusToolBar.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/6/29.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit

class YHStatusToolBar: UIView {
    
    var retweetButton: UIButton?
    var commentButton: UIButton?
    var unlikeButton: UIButton?
    
    //供外界赋值
    var status:YUStatus?{
        didSet{
            retweetButton?.setTitle(status?.repostsStr, forState: UIControlState.Normal)
            commentButton?.setTitle(status?.commentsStr, forState: UIControlState.Normal)
            unlikeButton?.setTitle(status?.attitudesStr, forState: UIControlState.Normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        self.backgroundColor = UIColor.whiteColor()
        
        retweetButton = addChildButtons("timeline_icon_retweet", title: "转发")
        commentButton = addChildButtons("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButtons("timeline_icon_unlike", title: "赞")
        
        //分隔线
        let line1 = addChildLine()
        let line2 = addChildLine()
        
        line1.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetButton!.snp_trailing)
        }
        line2.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton!.snp_trailing)
        }
        
        //添加约束
        retweetButton?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        })
        commentButton?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.left.equalTo(retweetButton!.snp_right)
            make.width.equalTo(unlikeButton!)
        })
        unlikeButton?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.bottom.right.equalTo(self)
            make.left.equalTo(commentButton!.snp_right)
        })
        
    }
    
    
    private func addChildButtons(imgName: String,title: String) -> UIButton{
        let button = UIButton()
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        addSubview(button)
        return button
    }
    
    //创建分隔线的方法
    private func addChildLine() -> UIImageView{
        let img = UIImageView(imgName: "timeline_card_bottom_line")
        addSubview(img)
        return img
    }

}
